
import MultipleInput from '@/components/question/MultipleInput.vue'
import { v4 as uuidv4 } from 'uuid'

export const QnTypes = ['singleSelect', 'multipleSelect']

export const QnTypeMap = {
    singleSelect: '单选',
    multipleSelect: '多选'
}
const QnJson: any = {
    singleSelect: {
        title: '请选择一个选项',
        data: [{
            label: '选项1',
            value: '选项1'
        }, {
            label: '选项2',
            value: '选项2',
        }]
    },
    multipleSelect: {
        title: '请选择',
        data: [{
            label: '选项1',
            value: '选项1'
        }, {
            label: '选项2',
            value: '选项2',
        }]
    },
    dropdown: {
        title: '请选择',
        data: [{
            label: '选项1',
            value: '选项1'
        }, {
            label: '选项2',
            value: '选项2',
        }]
    },
    cascader: {
        title: '请选择',
        data: [{
            label: '选项1',
            value: '选项1',
            children: [{
                label: '选项1-1',
                value: '选项1-1',
            }]
        }, {
            label: '选项2',
            value: '选项2',
        }]
    },
    picSelect: {
        title: '请选择',
        data: []
    },
    matrixSelect: {
        title: '请选择',
        data: {
            rows: [{
                label: '矩阵行1',
            }, {
                label: '矩阵行2',
            }],
            cols: [{
                label: '选项1'
            }, {
                label: '选项1'
            }],
        }
    },

    input: {
        title: '请输入',
    },
    multipleInput: {
        data: [{
            title: '填空1'
        }, {
            title: '填空2'
        }]
    },
    horizontalInput: {
        template: `姓名________年龄____岁<br/>
        联系方式___________`
    },
    matrixInput: {
        title: '请填写以下内容',
        data: {
            rows: [{
                label: '矩阵行1',
            }, {
                label: '矩阵行2',
            }],
            cols: [{
                label: '填空1'
            }, {
                label: '填空1'
            }],
        }
    },

}

let instance = null
class QnRenderEngine {
    qnData: any[] = [];
    static instance: any;
    constructor() {
        if (!QnRenderEngine.instance) {
            QnRenderEngine.instance = this;
        }
        return QnRenderEngine.instance;
    }
    #setValue(data: any) {
        data.data?.forEach((item: any) => {
            item.value = uuidv4()
            if (item.children) {
                this.#setValue(item.children)
            }
        })
    }
    #initQuestionSetting(data: any) {
        data.setting = {
            showIndex: true,
            layout: 3
        }
    }
    render(group: string, type: string) {
        const data: any = JSON.parse(JSON.stringify(QnJson[type]));
        if (group === 'select') {
            if (type === 'matrixSelect') {
                data.data.rows.forEach((item: any) => {
                    item.value = uuidv4()
                })
                data.data.cols.forEach((item: any) => {
                    item.value = uuidv4()
                })
            } else {
                // 给选项option添加唯一值
                this.#setValue(data)
            }
        }
        data.type = type;
        data.id = uuidv4()
        this.qnData.push(data);
        this.#initQuestionSetting(data)
        return data
    }
}

export default QnRenderEngine;